/*
 * @Author: your name
 * @Date: 2021-08-03 17:06:33
 * @LastEditTime: 2021-08-06 20:08:31
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \hrsaas-86\src\permission.js
 */
// 权限处理的页面   这个模块自己写
import router from "@/router";
import store from "@/store";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
//前置导航守卫
const whiteList = ["/login", "/404"];
router.beforeEach(async (to, from, next) => {
  NProgress.start();
  const token = store.getters.token;
  if (token) {
    if (to.path === "/login") {
      next("/");
      NProgress.done();
    } else {
      if (!store.state.user.userInfo.userId) {
        const res = await store.dispatch("user/getUserInfo");
        console.log(res);
      }
      next();
    }
  } else {
    if (whiteList.indexOf(to.path !== -1)) {
      next();
    } else {
      next("/login");
      NProgress.done();
    }
  }
});

//后置导航守卫
router.afterEach((to, from) => {
  NProgress.done();
});
